<template>
  <div>
    <el-card class="search-card">
      <basic-table-form
        ref="formData"
        :colNumOfRow="4"
        :formItemWidth="'115px'"
        :formColumns="formItemList"
        :dataSource="formData"
        :defaultExpandStatus="false"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
      >
      </basic-table-form>
    </el-card>
    <el-card class="table-card">
      <basic-table
        :showSelectCol="false"
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :tableHeight="460"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      >
        <el-row slot="content_context" type="flex" justify="start">
          <BtnRender :render="renderBtnsBar" />
        </el-row>
      </basic-table>
    </el-card>
  </div>
</template>
<script>
import BasicTableForm from '@/components/supplier/BasicForm';
import BasicTable from '@/components/supplier/BasicTable';
import { FORM_ITEM_LIST, TABLE_COLUMNS } from './constants';
import { getBtnsId } from '@/libs/btnsPermission';
import BasicPage from '@/mixins/supplier/basicPage';
import {
  getPurchaseStatisticsService,
  exportPurchaseStatisticsService
} from '@api/supplier/dataStatisticsQuery/purchaseStatistics';
import { isResponseSuccess, getHasIndexTableData } from '@/pages/supplier/utils/bizUtils';
import { createExcelByExport } from '@/utils/bizUtils';
import { getBtnsBar } from '@supplier/utils/btnsUtils';
import BtnRender from '@/components/JSXRender/BtnRender';

export default {
  mixins: [ BasicPage ], // 分页数据及方法 表单基础方法
  components: {
    BasicTableForm,
    BasicTable,
    BtnRender
  },
  data () {
    return {
      formItemList: FORM_ITEM_LIST,
      tableInfo: {
        list: [],
        columns: TABLE_COLUMNS,
        loading: false
      },
      formData: {
        supplierCode: '', // 供应商代码
        supplierName: '', // 供应商名称
        belongDepartment: '', // 采购归属
        belongArea: '', // 管理区域
        createUser: '', // 接待人
        businessCounterpart: '', // 业务对接人
        supplierClassify: '' // 供应商分类
      }
    };
  },
  created () {
    this.getTableData();
  },
  methods: {
    // 渲染按钮操作栏
    renderBtnsBar (h) {
      const btnsBar = [];
      if (this.supplierExportConfig.dataStatistics) {
        btnsBar.unshift({
          label: '导出',
          method: this.handleExport,
          loading: this.exportLoading,
          size: 'mini',
          type: 'primary',
          hasAuth: true
        })
      }
      return getBtnsBar(h, btnsBar, this.showBtn);
    },
    // 获取表格数据
    getTableData () {
      let data = {
        ...this.formData,
        ...this.pagination
      };
      sessionStorage.setItem('buttonId', getBtnsId('查询'));
      this.tableInfo.loading = true;
      getPurchaseStatisticsService(data).then((res) => {
        this.tableInfo.loading = false;
        if (isResponseSuccess(res)) {
          const { records, total } = res.result;
          this.tableInfo.list = getHasIndexTableData(records, this.pagination);
          this.pagination.total = total;
        }
      }).catch(() => {
        this.tableInfo.loading = false;
      });
    },
    // 导出
    handleExport () {
      this.exportLoading = true;
      exportPurchaseStatisticsService(this.formData).then((res) => {
        this.exportLoading = false;
        createExcelByExport({ excelTitle: '进货额统计信息', res })
      })
    }
  }
};
</script>
